package com.xiaobai.netease_news.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xiaobai.netease_news.R;
import com.xiaobai.netease_news.utils.ViewHolder;

import java.util.List;
import java.util.Map;

/**
 * Created by Android Studio
 * Project: Netease_News
 * Author: Jack
 * Email: jackfangqi1314@gmail.com/jackfangqi@163.com
 * Date: 2015/6/25
 */
public class CommonGridViewAdapter extends BaseAdapter {

    private Context context;
    private List<Map<String, Integer>> icons;
    private LayoutInflater inflater;

    public CommonGridViewAdapter(Context context, List<Map<String, Integer>> icons) {
        this.context = context;
        this.icons = icons;

        if (context != null) {
            inflater = LayoutInflater.from(context);
        }
    }

    @Override
    public int getCount() {
        if (icons != null) {
            return icons.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (icons != null) {
            return icons.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null)
            convertView = inflater.inflate(R.layout.common_grid_view_layout, parent, false);

        ImageView icon = ViewHolder.getView(convertView, R.id.more_menu_icon);
        TextView desc = ViewHolder.getView(convertView, R.id.more_menu_desc);

        Map<String, Integer> map = icons.get(position);
        icon.setImageResource(map.get("icon"));
        desc.setText(map.get("desc"));

        return convertView;
    }
}
